<?php
/**
 * Created by PhpStorm.
 * User: 11893
 * Date: 2018/9/13
 * Time: 13:36
 */

namespace App\Libs;

use App\Exceptions\InternalException;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Prophecy\Exception\Doubler\MethodNotFoundException;

class CLog
{
    protected static $instance;
    /**
     * @var array
     */
    protected static $logger = [];

    protected function info($message, $file = 'laravel')
    {
        dd($this->getLog(Logger::INFO, $file)->info(123));
    }


    /**
     * @param $level
     * @param string $file
     * @param bool $day
     * @return Logger
     */
    protected function getLog($level, $file = 'laravel', $day = true): Logger
    {
        $driver = $level . $file;
        if (empty(self::$logger[$driver])) {
            if ($day) {
                $file .= '_' . date('Y-m-d');
            }
            $handler = new StreamHandler($file . '.log', $level);
            $logger = new Logger($handler);
            $handler->setFormatter(tap(new LineFormatter(null, null, true, true), function ($formatter) {
                $formatter->includeStacktraces();
            }));
            self::$logger[$driver] = $logger;
        }
        return self::$logger[$driver];
    }

    public static function __callStatic($name, $arguments)
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        if (!method_exists(self::$instance, $name)) {
            throw new MethodNotFoundException("method $name not found");
        }
        self::$instance->$name(...$arguments);
    }
}